package obj;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;


import util.Rect;
import util.Settings;

/**
 * Sprite class.
 * @author VincentLy
 */
public class Sprite extends Rect{

	private boolean hit;
	private Image pic;	
	private Platform on;
	private float xprev,yprev;
	private float xvel,yvel;		
	private int dir;	
	private int jumps;

	public Sprite(float x,float y,int d,Image i) throws SlickException{
		super(x+15,y,i.getWidth()-40,i.getHeight()-40,5);		
		xvel = 0;
		yvel = 0;
		dir = d;	
		pic = i;
		jumps = 2;
		hit = false;
		on = null;
	}		

	/**
	 * @return previous x location
	 * @author VincentLy
	 */
	public float xPrev(){return xprev;}

	/**
	 * @return previous y location
	 * @author VincentLy
	 */
	public float yPrev(){return yprev;}

	/**
	 * @return x velocity
	 * @author VincentLy
	 */
	public float xvel(){return xvel;}

	/**
	 * @return y velocity
	 * @author VincentLy
	 */
	public float yvel(){return yvel;}	

	/**
	 * @return direction
	 * @author VincentLy
	 */
	public int dir(){return dir;}

	/**
	 * @return sprite's image
	 * @author VincentLy
	 */
	public Image img(){return pic;}	

	/**
	 * @return the platform the sprite is on
	 * @author VincentLy
	 */
	public Platform platform(){return on;}	

	/**
	 * @return the number of remaining jumps
	 * @author VincentLy
	 */
	public int jumps(){return jumps;}	

	/**
	 * @param j - jumps
	 * @author VincentLy
	 */
	public void setJumps(int j){jumps = j;}

	/**
	 * @param p - platform
	 * @author VincentLy
	 */
	public void setPlatform(Platform p){on = p;}

	/**
	 * @param v - velocity
	 * @author VincentLy
	 */
	public void setXVel(float v){xvel = v;}

	/**
	 * @param v - velocity
	 * @author VincentLy
	 */
	public void setYVel(float v){yvel = v;}		
	
	/**
	 * @return whether sprite is hit
	 * @author VincentLy
	 */
	public boolean isHit(){return hit;}
	
	/**
	 * @param b - if sprite is hit
	 * @author VincentLy
	 */
	public void setHit(boolean b){hit = b;}

	/**
	 * Updates the sprite physics.
	 * @author VincentLy
	 */
	public void update(){			
		yvel-=2;
		move((float) (x() + xvel*Settings.dt),(float) (y()-yvel*Settings.dt));	
	}		

	/**
	 * Updates previous locations.
	 * @author VincentLy
	 */
	public void update2(){
		xprev = x();
		yprev = y();
	}

	/**
	 * @param d - direction
	 * @author VincentLy
	 */
	public void setDir(int d){dir = d;}

	/**
	 * Draws the sprite.
	 * @author VincentLy
	 */
	public void draw(Graphics g){pic.draw(x()-dir*(width()/2+15),top()-30,dir*pic.getWidth(),pic.getHeight());}
}